<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{number}}.{{title}}</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-language_tools.js" charset="utf-8"></script>
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <link rel="stylesheet" href="../css/navbar.css">
    <style>
        /* 基本样式 */
        :root {
            --primary-color: #2d3e50;
            --secondary-color: #f9fafc;
            --accent-color: #2d8cf0;
            --hover-color: #1971c2;
            --text-dark: #333;
            --text-light: #666;
            --white: #ffffff;
            --shadow: rgba(0, 0, 0, 0.1);
        }

        body {
            font-family: Arial, sans-serif;
            margin: 0;
            background-color: var(--secondary-color);
            color: var(--text-dark);
        }

        .container {
            width: 100%;
            max-width: 100%;
            margin: auto;
        }

        /* 导航栏 */
        /* 内容区域 */
        .part1 {
            display: flex;
            padding: 20px;
        }

        .left_desc {
            width: 50%;
            padding-right: 20px;
            overflow-y: auto;
        }

        .left_desc h3 {
            font-size: 1.6em;
            color: var(--primary-color);
            margin-bottom: 10px;
        }

        .left_desc pre {
            font-size: 1em;
            color: var(--text-light);
            background-color: var(--white);
            padding: 15px;
            border-radius: 5px;
            box-shadow: 0 4px 8px var(--shadow);
        }

        /* 代码编辑器 */
        .right_code {
            width: 50%;
            padding-left: 20px;
        }

        #code {
            height: 600px;
            border: 1px solid var(--shadow);
            border-radius: 5px;
            box-shadow: 0 4px 8px var(--shadow);
        }

        /* 按钮样式 */
        .part2 {
            display: flex;
            justify-content: flex-end;
            padding: 20px;
        }

        .btn-implement,
        .btn-submit {
            width: 120px;
            height: 50px;
            font-size: 1em;
            font-weight: bold;
            border-radius: 8px;
            cursor: pointer;
            transition: background-color 0.3s;
            margin-left: 15px;
        }

        .btn-implement {
            background-color: #ffffff;
            color: var(--text-dark);
            border: 2px solid var(--accent-color);
        }

        .btn-implement:hover {
            background-color: var(--hover-color);
            color: var(--white);
        }

        .btn-submit {
            background-color: #26bb9c;
            color: var(--white);
            border: none;
        }

        .btn-submit:hover {
            background-color: #1a9a7d;
        }

        /* 输出结果 */
        .result {
            margin-top: 10px;
            font-size: 0.9em;
            color: var(--text-dark);
        }
    </style>
</head>

<body>

    <div class="container">
        <!-- 导航栏 -->
        <div class="navbar">
            <div class="left">
                <a href="/">首页</a>
                <a href="/all_questions.html">编程题库</a>
            </div>
            <div class="center">
                <a href="/html/job_seek.html">求职</a>
                <a href="/html/learning.html">学习</a>
                <a href="/html/discuss.html">讨论</a>
                <a href="/html/question_bank.html">知识题库</a>
                <a href="/html/ai_qa.html">AI问答</a>
            </div>
            <div class="right">
                <a href="/html/register.html">注册</a>
                <a href="/html/login.html">登录</a>
                <a href="/html/userInfo.html" class="toRight">个人信息</a>
            </div>
        </div>

        <!-- 内容区域：题目描述和代码编辑器 -->
        <div class="part1">
            <div class="left_desc">
                <h3><span id="number">{{number}}</span>. {{title}} <small>{{level}}</small></h3>
                <pre>{{desc}}</pre>
            </div>
            <div class="right_code">
                <pre id="code" class="ace_editor"><textarea class="ace_text-input">{{pre_code}}</textarea></pre>
            </div>
        </div>

        <!-- 操作区：执行和提交按钮 -->
        <div class="part2">
            <div class="result"></div>
            <button class="btn-implement" onclick="implement()">执行代码</button>
            <button class="btn-submit" onclick="submit()">提交代码</button>
        </div>
    </div>

    <script>
        // 初始化 ACE 编辑器
        const editor = ace.edit("code");
        editor.setTheme("ace/theme/monokai");
        editor.session.setMode("ace/mode/c_cpp");
        editor.setFontSize(16);
        editor.getSession().setTabSize(4);
        editor.setReadOnly(false);
        editor.setOptions({
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true
        });

        function submit() {
            // 收集页面数据
            const code = editor.getSession().getValue();
            const number = $("#number").text();
            const judge_url = `/judge/${number}`;

            // 发起 Ajax 请求
            $.ajax({
                method: 'POST',
                url: judge_url,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                data: JSON.stringify({ 'code': code, 'input': '' }),
                success: function (data) {
                    show_result(data);
                },
                error: function (jqXHR) {
                    console.error("请求失败:", jqXHR.statusText);
                    show_result({ status: -1, reason: "网络请求失败，请稍后重试。" });
                }
            });
        }

        function show_result(data) {
            const result_div = $(".result");
            result_div.empty();

            // 显示运行状态和原因
            $("<p>", { text: data.reason }).appendTo(result_div);

            if (data.status === 0) {
                // 请求成功，显示输出
                if (data.stdout) {
                    $("<pre>", { text: data.stdout }).appendTo(result_div);
                }
                if (data.stderr) {
                    $("<pre>", { text: data.stderr }).appendTo(result_div);
                }
            }
        }
    </script>

</body>


</html>
